﻿using System;
using System.Windows;

namespace WpfDownload
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{

		//private DownloadHandler manager;

		public MainWindow()
		{
			InitializeComponent();

			txtDestination.Text = System.IO.Path.GetTempPath();
			txtUriList.AcceptsReturn = true;
			txtUriList.Clear();
		}

		private void btnDownload_Click(object sender, RoutedEventArgs e)
		{
			var manager = new DownloadHandler(txtDestination.Text, txtPrefix.Text);
			var strings = txtUriList.Text.Split("\r\n".ToCharArray());
			foreach (var s in strings)
			{
				if (!string.IsNullOrWhiteSpace(s))
				{
					manager.Add(new Uri(s.Trim()));
				}
			}

			lbTasks.ItemsSource = manager.Tasks;

			manager.Completed += delegate
			                     	{
															lbTasks.Items.Refresh();
			                     	};
			manager.ProgressChanged += delegate
			                           	{
																		lbTasks.Items.Refresh();
			                           	};
			manager.Start();
		}

		private void ShowAboutBox(object sender, RoutedEventArgs e)
		{
			var window = new AboutDownloader(this);
			window.Show();

		}

		private void SelectDestinationFolder(object sender, RoutedEventArgs e)
		{
			var dialog = new FolderBrowserDialogEx();
			dialog.Description = "Select a destination location to drop the downloaded files.";
			dialog.ShowNewFolderButton = true;
			dialog.SelectedPath = txtDestination.Text;
			dialog.ShowFullPathInEditBox = true;

			if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				txtDestination.Text = dialog.SelectedPath;
			}
		}
	}
}
